package com.qing.springboot01.controller;

import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileInputStream;

@RestController
public class DownLoadController {

    @Value("${reggie.path}")
    private String path;

    @RequestMapping("/common/download")
    public void download(String name, HttpServletResponse response) throws Exception {
        response.setContentType("image/jpeg");
        ServletOutputStream outputStream = response.getOutputStream();
        FileInputStream fis = new FileInputStream(new File(path + name));
//       读流
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = fis.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
            outputStream.flush();
        }
        outputStream.close();
        fis.close();

    }
}
